package com.sky.web.app;

import com.sky.context.ThreadLocalUtil;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
public class AppAddressController {
    @Autowired
    private AddressService addressService;
    @GetMapping("list")
    public Result findById(){
        AddressBook addressBook=new AddressBook();
        addressBook.setUserId(ThreadLocalUtil.getCurrentId());
       List<AddressBook> list = addressService.findList(addressBook);
        return Result.success(list);
    }

    @PostMapping
    public Result save(@RequestBody AddressBook addressBook){
            addressService.save(addressBook);
        return Result.success();
    }

    @GetMapping("default")
    public Result findDefaultAddress(){
       AddressBook addressBook = addressService.findDefaultAddress();
        return Result.success(addressBook);
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable Long id){
        AddressBook addressBook = addressService.findById(id);
        return Result.success(addressBook);
    }

    @PutMapping
    public Result updateAddressBook(@RequestBody AddressBook addressBook){
        addressService.updateAddressBook(addressBook);

        return Result.success();
    }

    @DeleteMapping
    public Result deleteById(@RequestParam Long id){
        addressService.deleteById(id);

        return Result.success();
    }

    @PutMapping("default")
    public Result setDefault(@RequestBody AddressBook addressBook){
        addressService.setDefault(addressBook.getId());
        return Result.success();
    }
}
